﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using DigitalRubyShared;
using UnityEngine.UI;

public class LevelHideLevelId2 : MonoBehaviour
{
    public Text levelIdText;
    public Button loveButton;
    public Text loveText;
    //public 
    private readonly List<RaycastResult> raycast = new List<RaycastResult>();
    private Canvas canvas;
    private PanGestureRecognizer idMoveGesture;
    private Transform draggingId;
    private Vector2 dragOffset;
    public GameLevelItem gameLevelItem;

    private void IdGestureUpdated(GestureRecognizer gesture)
    {
        Debug.Log("Pan state " + gesture.State);

        if (gesture.State == GestureRecognizerState.Began)
        {
            PointerEventData p = new PointerEventData(EventSystem.current);
            p.position = new Vector2(gesture.FocusX, gesture.FocusY);
            raycast.Clear();
            EventSystem.current.RaycastAll(p, raycast);
            foreach (RaycastResult result in raycast)
            {
                if (result.gameObject.name.IndexOf("DragLevelText", System.StringComparison.OrdinalIgnoreCase) >= 0)
                {
                    // we have a letter!
                    Vector2 dragPos = FingersUtility.ScreenToCanvasPoint(canvas, new Vector2(gesture.FocusX, gesture.FocusY));
                    draggingId = result.gameObject.transform;
                    dragOffset = (Vector2)draggingId.position - dragPos;
                    break;
                }
            }
            if (draggingId == null)
            {
                gesture.Reset();
            }
        }
        else if (gesture.State == GestureRecognizerState.Executing)
        {
            Vector2 dragPos = FingersUtility.ScreenToCanvasPoint(canvas, new Vector2(gesture.FocusX, gesture.FocusY));
            Vector3 pos = draggingId.transform.position;

            // don't mess with the z
            pos.x = dragPos.x + dragOffset.x;
            //pos.y = dragPos.y + dragOffset.y;
            draggingId.transform.position = pos;
        }
        else if (gesture.State == GestureRecognizerState.Ended)
        {
            draggingId = null;
        }
    }

    private void Start()
    {
        levelIdText.text = gameLevelItem.levelId.ToString();
        loveButton.onClick.AddListener(() => {
            SoundManager.Instance.PlayAudio(SoundType.Click.ToString(), false);
            loveText.color = Color.black;
            gameLevelItem.TriggerResult(true);
        });
    }

    private void OnEnable()
    {
        canvas = GameCtrl.Instance.curCanvas;
        idMoveGesture = new PanGestureRecognizer();
        idMoveGesture.ThresholdUnits = 0.0f; // start right away
        idMoveGesture.StateUpdated += IdGestureUpdated;
        FingersScript.Instance.AddGesture(idMoveGesture);
    }

    private void OnDisable()
    {
        if (FingersScript.HasInstance)
        {
            FingersScript.Instance.RemoveGesture(idMoveGesture);
        }
    }

   
}
